package com.gfxy.modules.common.controller;

import com.gfxy.modules.GfxyUtils;
import com.gfxy.modules.common.service.IGfxyCommonService;
import com.gfxy.modules.common.service.IStaffTreeService;
import com.gfxy.modules.common.entity.TreeItemModel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.jeecg.common.api.vo.Result;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Api(tags="树查询")
@RestController
@RequestMapping("/gfxy/tree")
@Slf4j
public class TreeController {


    @Autowired
    private IStaffTreeService staffTreeService;

    @Autowired
    private IGfxyCommonService gfxyCommonService;

    @AutoLog(value = "科研管理-树部门或员工")
    @ApiOperation(value="科研管理-树部门或员工", notes="科研管理-树部门或员工")
    @GetMapping(value = "/educationTree")
    public Result<?> getEducationTree(HttpServletRequest req) {
        String rolesStr = GfxyUtils.getRoles(req);
        List<TreeItemModel> resultList = null;
        if(this.isAllDataPermission(rolesStr)){
            resultList = this.gfxyCommonService.getTreeModelForOrg();
        } else {
            String orgCode = GfxyUtils.getOrgCode(req);
            resultList = this.gfxyCommonService.getTreeModelByOrgCode(orgCode);
        }
        return Result.OK(resultList);
    }

    @AutoLog(value = "科研管理-树部门或员工")
    @ApiOperation(value="科研管理-树部门或员工", notes="科研管理-树部门或员工")
    @GetMapping(value = "/educationTreeStaff")
    public Result<?> getEducationTreeStaff(@RequestParam(name="orgCode",required=true) String orgCode, HttpServletRequest req) {
        List<TreeItemModel> resultList = this.gfxyCommonService.getTreeModelTeachingTeacherByOrgCode(orgCode);
        return Result.OK(resultList);
    }

    @AutoLog(value = "科研管理-树部门或员工")
    @ApiOperation(value="科研管理-树部门或员工", notes="科研管理-树部门或员工")
    @GetMapping(value = "/educationTreeAllStaff")
    public Result<?> getEducationTreeAllStaff(@RequestParam(name="orgCode",required=true) String orgCode, HttpServletRequest req) {
        List<TreeItemModel> resultList = this.gfxyCommonService.getTreeModelTeacherByOrgCode(orgCode);
        return Result.OK(resultList);
    }


    private boolean isAllDataPermission(String rolesStr) {
        if(rolesStr.contains("institute-operater")){
            return false;
        } else {
            return true;
        }
    }
}
